﻿import php.Lib;
import php.Web;
import php.db.Manager;

class Main {

	public static function main() {
		var params = Web.getParams();
		try {
			var connection = php.db.Mysql.connect(Configuration.getDbParams());
			connection.request("SET NAMES 'utf8';");
			Manager.cnx = connection;
			
		} catch (e: Dynamic) {
			trace(e);
			
			return;
		}
		
		switch (params.get('action')) {
			case "delete": 
				var id = Std.parseInt(params.get('id'));
				deletePet(id);
			case "add":
				var owner = params.get('owner');
				var species = params.get('species');
				var name = params.get('name');
				addPet(owner, species, name);
			case "list":
				listPets();
			default:			
		}
		
	}
	
	private static function deletePet(id: Int) {
		Pet.manager.delete( { id: id } );
		Lib.print("pet was deleted");
	}
	
	private static function addPet(owner: String, species: String, name: String ) {
		var pet = new Pet(owner, species, name);
		pet.insert();
		Lib.print("pet was inserted");
	}
	
	private static function listPets() {
		var pets = Pet.manager.all();
		for (pet in pets) {
			Lib.print(pet.id + " " + pet.owner + " " + pet.species + " "+ pet.name+"<br>");
		}
	
	}
	
}